/**
 * Copyright(C) 2012 Luvina Software Company
 *
 * TelValidator.java, 10/1/2013, bacdv
 */
package net.luvina.manageuser.validator;

import java.text.MessageFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import net.luvina.manageuser.util.Message;

/**
 * TelValidator
 *
 * @author bacdv
 *
 */
public class TelValidator implements Validator {

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * javax.faces.validator.Validator#validate(javax.faces.context.FacesContext
	 * , javax.faces.component.UIComponent, java.lang.Object)
	 */
	public void validate(FacesContext context, UIComponent component, Object obj)
			throws ValidatorException {
		String tel = (String) obj;
		String labelItem = (String) component.getAttributes().get("label");
		int max = 14;
		String strFormat = "xxxx-xxxx-xxxx";
		Pattern patt = Pattern.compile("^[0-9]{1,4}-[0-9]{1,4}-[0-9]{1,4}$");
		Matcher match = patt.matcher(tel);
		boolean matchFound = match.find();
		if (!matchFound) {
			FacesMessage message = new FacesMessage();
			message.setSummary(MessageFormat.format(Message.getString("ER005"),
					labelItem, strFormat));
			throw new ValidatorException(message);
		}
		if (tel.length() > max) {
            FacesMessage message = new FacesMessage();
            message.setSummary(MessageFormat.format(Message.getString("ER006"),
                    max, labelItem));
            throw new ValidatorException(message);
        }
	}

}
